/*
 * WeightDialogCommand.java
 *
 * Created on 11 maj 2007, 22:58
 */

package edu.uj.petri.commands;

import edu.uj.petri.document.Document;
import edu.uj.petri.model.NormalArc;
import java.util.ResourceBundle;
import javax.swing.JOptionPane;

/**
 * Shows dialog of changing weight of an arc and changes it.
 * @author Piotrek
 */
public class WeightDialogCommand extends Command {
    
    private NormalArc arc;
    
    /** Creates a new instance of WeightDialogCommand */
    public WeightDialogCommand(NormalArc arc) {
        super(true); // do not remember on command stack
        this.arc = arc;
    }

    public String toString() {
        return ResourceBundle.getBundle("edu/uj/petri/messages").getString("zmiana_wagi_krawedzi");
    }

    void execute() {
        int oldWeight = arc.getWeight();
        String response = JOptionPane.showInputDialog( null, ResourceBundle.getBundle("edu/uj/petri/messages").getString("zmiana_wagi_krawedzi_prompt"), oldWeight);
        if (response != null && !response.equals(""))
        {
            try
            {   
                int newWeight = Integer.parseInt( response );
                
                if (newWeight < 1) {
                    JOptionPane.showMessageDialog( null, "invalid weight");
                    return;
                }
                
                Document.getActiveDocument().execute(new SetArcWeightCommand(arc, newWeight));
            } 
            catch (NumberFormatException e)
            {
                JOptionPane.showMessageDialog( null, "invalid weight");
            }
        }
    }

    void undo() {
        throw new IllegalArgumentException(ResourceBundle.getBundle("edu/uj/petri/messages").getString("cannot_be_undone"));
    }

    boolean canBeCombined(Command c) {
        return false;
    }

    private static final long serialVersionUID = 2747492258596028951L;
    
}
